package com.malou.client.ventas.cambio;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;
import com.extjs.gxt.ui.client.widget.grid.filters.DateFilter;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.grid.filters.DateFilter.DateFilterMessages;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;
import com.malou.client.util.Utilities;

public class DevolucionPrendaPanel extends ContentPanel{
	private MalouConstants constants = GWT.create(MalouConstants.class);
	private ToolBar toolBar;
	private Button nuevoDevolucion;		
	public  DevolucionPrendaDialog dialog;
	Integer selectedSize;
	private BeanModel selectedModel;
	
	public DevolucionPrendaPanel(){
		Cambios.getPaginateDevoluciones();
     	Cambios.getListDevoluciones();

		final PagingToolBar toolBarPag = new PagingToolBar(Utilities.itemsPerPage);
		toolBarPag.bind(Cambios.loaderDevolucion);
		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
        centerData.setMargins(new Margins(0));
           
        RowNumberer rc = new RowNumberer();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();	
		final CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
		columns.add(rc);
		columns.add(sm.getColumn());
		columns.add(new ColumnConfig("prendaingreso",constants.montorecibir(), 100));
		columns.add(new ColumnConfig("cantidad_ingreso",constants.montodevolver(), 100));
		columns.add(new ColumnConfig("montoingreso",constants.montodevolver(), 100));
		columns.add(new ColumnConfig("fecha", constants.fecha(),150));
			
		ColumnModel cm = new ColumnModel(columns);
		
		Grid<BeanModel> gridCambios = new Grid<BeanModel>(Cambios.storeDevolucion, cm);  
        gridCambios.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {  
          public void handleEvent(GridEvent<BeanModel> be) {  
        	  Cambios.loaderDevolucion.load(0, Utilities.itemsPerPage);
          }  
        });
        
        gridCambios.setSelectionModel(sm);
        gridCambios.setLoadMask(true);  
        gridCambios.setBorders(true);
                
        setFrame(true);  
        setCollapsible(true);  
        setAnimCollapse(false);  
        
        setIcon(IconHelper.createPath("images/icons/table.png"));  
        setHeading(constants.gastoLista());  
        setLayout(new BorderLayout());
        
        toolBar = new ToolBar();
        
        nuevoDevolucion = new Button(constants.nuevoCambio(), IconHelper.createPath("images/icons/addstock.png"));
        
        dialog = new DevolucionPrendaDialog();
        SelectionListener<ButtonEvent> sl_new;       
        sl_new = new SelectionListener<ButtonEvent>(){
        	
        	@Override
			public void componentSelected(ButtonEvent ce) {	
				dialog.clearPrincipal();				
			    dialog.show();			    
			}
        };
        nuevoDevolucion.addSelectionListener(sl_new);
        toolBar.add(nuevoDevolucion);             
        
        setTopComponent(toolBar);             
        
        GridFilters filters = new GridFilters();
        filters.getMessages().setFilterText(constants.filtros());
        
        StringFilter prendaIngresoFilter = new StringFilter("prendaingreso");
        StringFilter prendaSalidaFilter = new StringFilter("prendasalida");
        DateFilter fechaFilter = new DateFilter("fecha");
        
        DateFilterMessages dm= new DateFilterMessages();
        dm.setAfterText(constants.despues());
        dm.setBeforeText(constants.antes());
        dm.setOnText(constants.del());
        fechaFilter.setMessages(dm);
        
        filters.addFilter(prendaIngresoFilter);        
        filters.addFilter(prendaSalidaFilter);        
                
        gridCambios.addPlugin(filters);
    
        
        setHeaderVisible(false);	
        setLayout(new FitLayout());	
        add(gridCambios);
        setBottomComponent(toolBarPag);
        
	}
	
	public void setPermisos(List<String> permisos){
		
		boolean add = permisos.contains("realizar");		
		
		if(!add){
			toolBar.setVisible(false);
		}		
	}
}
